#include "myLog4.h"
#include <iostream>
#include <thread>
#include "interationfastapi.hpp"
#include "interationredis.hpp"
#include "interationjson.hpp"
#include "config.hpp"
#include "post.hpp"
#include "interationboard.hpp"

extern ThreadSafeQueue<std::string> uavExecuteCommandsQueue;
int serverSocketReceiverfd;
sockaddr_in serverAddrReceiver;

void initializemain();

int main(){
    initializemain();
    std::string server_ip = "192.168.10.11";  // 服务端IP地址
    int server_port = 5000;               // 服务端端口号
    TCPClient client(server_ip, server_port);  // 创建客户端并连接服务器
    LOG_INFO("[log4]" << LOG4CPLUS_TEXT("111: "));
    //redis存取测试成功,订阅信息存储
    std::string redistest = "hello world";
    subscriptionInfo2Redis("233", redistest);
    std::cout << getSingnalSubscriptionInfoFromRedis("201") << std::endl;

    //请求回调函数测试成功，指令处理结果
    string response;
    std::string data = R"({"TIME_NOW":{"formatted_time":"2024-06-13 10:57:25","timestamp":1718247445969},"TOPIC_ALTITUDE_BAROMETER":115.82001495361328,"TOPIC_ALTITUDE_FUSIONED":114.89781188964844,"TOPIC_ALTITUDE_OF_HOMEPOINT":114.8978042602539,"TOPIC_AVOID_DATA":{"back":28.670000076293945,"backHealth":1,"down":0.19653667509555817,"downHealth":1,"front":24.03999900817871,"frontHealth":1,"left":12.639999389648438,"leftHealth":1,"reserved":0,"right":16.100000381469727,"rightHealth":1,"up":100.0,"upHealth":1},"TOPIC_BATTERY_INFO":{"capacity":1159,"current":1197,"percentage":11,"voltage":44158},"TOPIC_CONTROL_DEVICE":{"controlMode":0,"deviceStatus":4,"flightStatus":1,"reserved":0,"vrcStatus":0},"TOPIC_FLIGHT_ANOMALY":{"aircraftIsFalling":0,"atLeastOneEscDisconnected":0,"compassInstallationError":0,"escTemperatureHigh":0,"gpsYawError":0,"heightCtrlFail":0,"impactInAir":0,"imuInstallationError":0,"randomFly":0,"reserved":122627,"rollPitchCtrlFail":0,"strongWindLevel1":0,"strongWindLevel2":0,"yawCtrlFail":0},"TOPIC_GPS_DETAILS":{"GPScounter":9014,"NSV":27,"fix":3.0,"gnssStatus":1094.0,"hacc":589.0,"hdop":56.0,"pdop":98.0,"sacc":10.0,"usedGLN":13,"usedGPS":6},"TOPIC_GPS_FUSED":{"altitude":115.7433853149414,"latitude":0.5555389973171465,"longitude":2.0442449467919284,"visibleSatelliteNumber":26},"TOPIC_GPS_POSITION":{"x":1171266124,"y":318300376,"z":49249},"TOPIC_HEIGHT_FUSION":0.19653667509555817,"TOPIC_HOME_POINT_INFO":{"latitude":0.5555391066813905,"longitude":2.044245352226977},"TOPIC_STATUS_MOTOR_START_ERROR":0,"TOPIC_VELOCITY":{"data":{"Vector3f":{"x":-0.0010707249166443944,"y":0.0006772394408471882,"z":9.42658371059224e-05}},"info":{"VelocityInfo":{"health":1,"reserve":0}}},"flightStatus":3,"type":"0","uid":"201"})";
    // performPostRequest("http://172.21.2.6:8001/print", data, response);
    // std::cout << "请求状态:" << response << std::endl;

    //测试json相关操作，获取无人机GPS数据
    std::vector<std::string> uids = {"201", "202", "204"};
    std::cout << getMultiGpsFromRedis(uids) << std::endl;

    //测试获取fastapi指令数据，获取指令(集)
    std::thread receiverPythonThread(receivePythonData, serverSocketReceiverfd, serverAddrReceiver, std::ref(uavExecuteCommandsQueue));
    std::thread monitorBoardSendThread(monitorBoardSend, std::ref(uavExecuteCommandsQueue), std::ref(client));
    std::thread monitorBoardReceiveThread(monitorBoardReceive, std::ref(uavExecuteCommandsQueue), std::ref(client));
    receiverPythonThread.join();
    monitorBoardSendThread.join();
    monitorBoardReceiveThread.join();
}

void initializemain() {
    InitLogger(); // 初始化日志系统
    initializeRedis(); // 初始化redis

    serverSocketReceiverfd = socket(AF_INET, SOCK_STREAM, 0);
    if (serverSocketReceiverfd == -1) {
        LOG_ERROR("[log4]" << "创建socket失败");
        exit(1); // 创建 socket 失败，退出程序
    }
    serverAddrReceiver.sin_family = AF_INET;
    serverAddrReceiver.sin_port = htons(RECEIVEFROMPYTHONPORT); // 使用端口5001
    serverAddrReceiver.sin_addr.s_addr = INADDR_ANY; // 监听所有网卡上的连接
}
